<?php

use App\Http\Controllers\ChatGroup\Merchant\GroupMemberController;
use App\Http\Controllers\ChatGroup\Web\ChatGroupController;
use App\Http\Controllers\ChatGroup\Web\CustomerServeController as WebCustomerServeController;
use App\Http\Controllers\ChatGroup\Merchant\CustomerServeController as MerchantCustomerServeController;
use App\Http\Controllers\ChatGroup\Web\FaqController;
use App\Http\Controllers\ChatGroup\Web\TaskController;
use App\Http\Middleware\OpenShop\FeishuWebAuth;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ChatGroup\Web\ChatMessageController as WebChatMessageController;
use App\Http\Controllers\ChatGroup\Merchant\ChatMessageController as MerchantChatMessageController;

/**
 *
 * 群聊路由文件
 */

Route::get('test', function () {
    return '群聊路由文件';
});

// 商家app
Route::prefix('merchant')
     ->middleware(MerchantAppAuth::class)
     ->group(function () {

         // 客服
         Route::prefix('customerServe')->group(function () {

             // 聊天记录
             Route::get('getChatHistory', [MerchantCustomerServeController::class, 'getChatHistory']);

             // 进入会话
             Route::post('enterSession', [MerchantCustomerServeController::class, 'enterSession']);

             // 会话评价
             Route::post('appraise/{groupId}', [MerchantCustomerServeController::class, 'appraise'])
                  ->where('groupId', '[0-9]+');

             // 呼叫人工
             Route::post('callServe', [MerchantCustomerServeController::class, 'callServe']);

             // 取消呼叫
             Route::post('cancelCall', [MerchantCustomerServeController::class, 'cancelCall']);

             // 发送常见问题
             Route::post('sendFaq', [MerchantCustomerServeController::class, 'sendFaq']);

             // 常见问题列表
             Route::get('faqLists', [MerchantCustomerServeController::class, 'faqLists']);

             // 获得常见问题答案详情
             Route::get('faq/answer', [MerchantCustomerServeController::class, 'faqAnswer']);

             // 问题答案-增加有用、无用操作
             Route::post('faq/answer/operate', [MerchantCustomerServeController::class, 'faqAnswerOperate']);
         });

         // 成员管理
         Route::prefix('member')->group(function () {

             // 成员列表
             Route::get('list/{groupId}', [GroupMemberController::class, 'list'])->where('groupId', '[0-9]+');

             // 成员添加与移除
             Route::post('update/{groupId}', [GroupMemberController::class, 'updateExist'])->where('groupId', '[0-9]+');
         });

         // 消息
         Route::prefix('message')->group(function () {

             // 发送消息
             Route::post('send', [MerchantChatMessageController::class, 'send']);

             // 清空未读消息
             Route::post('clearUnread', [MerchantChatMessageController::class, 'clearUnread']);
         });
     });

// web端后台路由
Route::prefix('web')
     ->middleware([
         FeishuWebAuth::class,
         UserPermissionAuth::class . ':chatgroup'
     ])->group(function () {

        // 客服
        Route::prefix('customerServe')->group(function () {

            // 群聊列表
            Route::get('chatGroupList', [WebCustomerServeController::class, 'chatGroupList']);

            // 聊天记录
            Route::get('getChatHistory', [WebCustomerServeController::class, 'getChatHistory']);

            // 进入会话
            Route::post('enterSession', [WebCustomerServeController::class, 'enterSession']);

            // 客服介入
            Route::post('serveIn', [WebCustomerServeController::class, 'serveIn']);

            // 转接顾问
            Route::post('transferConsultant', [WebCustomerServeController::class, 'transferConsultant']);

            // 关闭会话
            Route::post('close', [WebCustomerServeController::class, 'close']);
        });

        // 消息
        Route::prefix('message')->group(function () {

            // 发送消息
            Route::post('send', [WebChatMessageController::class, 'send']);

            // 撤回消息
            Route::post('recall', [WebChatMessageController::class, 'recall']);

            // 发送常见问题
            Route::post('sendFaqDialog', [WebChatMessageController::class, 'sendFaqDialog']);
        });

        // 历史工单
        Route::get('history/statistics', [ChatGroupController::class, 'historyStatistics']);

        // 工单链接配置
        Route::get('workOrder/sendUrl', [ChatGroupController::class, 'workOrderSendUrl']);

        // 常见问题
        Route::prefix('faq')->group(function () {

            // 工单常见问题
            Route::get('/', [ChatGroupController::class, 'faq']);

            // 常见问题列表
            Route::get('list', [FaqController::class, 'list']);

            // 常见问题详情
            Route::get('info', [FaqController::class, 'info']);

            // 删除常见问题
            Route::post('del', [FaqController::class, 'del']);

            // 常见问题排序列表
            Route::get('sortList', [FaqController::class, 'sortList']);

            // 常见问题排序更新
            Route::post('updateSort', [FaqController::class, 'updateSort']);

            // 常见问题更新
            Route::post('faqUpdate', [FaqController::class, 'faqUpdate']);

            // 常见问题下载模版
            Route::get('downloadTemplate', [FaqController::class, 'downloadTemplate']);

            // 导入 Check
            Route::post('fileCheck', [FaqController::class, 'fileCheck']);

            // 工单, 功能开通链接获取
            Route::get('getWorkOrderUrl', [FaqController::class, 'getWorkOrderUrl']);

        });
        // 常见问题
        Route::prefix('task')->group(function () {

            // 获取门店详情
            Route::get('getShopInfo', [TaskController::class, 'getShopInfo']);

            // 日常任务创建
            Route::post('addTask', [TaskController::class, 'addTask'])->middleware('customerThrottle:1,5');
        });
    });
